<?php

namespace app\common\cachemodel\zl2;

use app\common\model\BaseCache;
use think\Log;
use XznsTools\cache\CacheManage;

class Zl2AstrFortuneText extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl2_astr_fortune_text';

    protected $cacheModel = 'astro_fortune.astr_fortune_text';

    /**
     * 更新 星运内容 词条ID内容
     * @param int $code
     * @param array $data
     * @return bool
     */
    public function UpdateData(int $code, array $data)
    {
        try
        {
            // 更新数据
            $this->where(['id' => $code])->update($data);
            // 更新缓存
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $code)->setCache($data);

            return true;
        }
        catch (\Exception $e)
        {
            Log::write('Zl2AstrFortuneText model => UpdateData()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');

            return false;
        }
    }
    
    /**
     * 批量获取 数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {
                $data = $this->where(['code' => ['in', $ids], 'status' => 1])->order("id {$sortType}")->column('title,explain,opportunity,challenge,extend,zan1,zan2,create_time', 'code');
            } else
            {
                return [];
            }
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['code', 'zan1', 'zan2', 'create_time']);

            if (isset($itemData['extend']))
            {
                $data[$index]['extend'] = json_decode($itemData['extend'], true);
            }
        }

        return $data;
    }
}